package com.bwc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwc.common.R;
import com.bwc.dao.DishDao;
import com.bwc.domain.Dish;
import com.bwc.domain.DishFlavor;
import com.bwc.dto.DishDto;
import com.bwc.service.DishFlavorService;
import com.bwc.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        // 这里是以为父类继承了子类，所以父类的操作能正常进行
        this.save(dishDto);
        //在保存时候，由于雪花算法，id自动生成，所以现在的id已经可以获取了，不用再次去设置菜品的id
        //ID是父类的属性，所以在保存父类的时候，产生的就是父类的ID
        Long dishId = dishDto.getId();

        List<DishFlavor> dishFlavorList = dishDto.getFlavors();

        //给每一个dishFlavor的dishID赋值
//        dishFlavorList.forEach(dishFlavor->dishFlavor.setDishId(dishId));
        dishFlavorList = dishFlavorList.stream().map((item)->{
                  item.setDishId(dishId);
                  return item;
                }).collect(Collectors.toList());

        dishFlavorService.saveBatch(dishFlavorList);

    }

    @Override
    public DishDto getDtoById(Long id) {
        Dish dish = this.getById(id);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper =new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        // 这里是以为父类继承了子类，所以父类的操作能正常进行
        this.updateById(dishDto);
        //在保存时候，由于雪花算法，id自动生成，所以现在的id已经可以获取了，不用再次去设置菜品的id
        //ID是父类的属性，所以在保存父类的时候，产生的就是父类的ID
        Long dishId = dishDto.getId();
        //给每一个dishFlavor的dishID赋值
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);

        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
        dishFlavorService.remove(lambdaQueryWrapper);
        dishFlavorList = dishFlavorList.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(dishFlavorList);

    }

    @Override
    public void removeByIdWithFlavor(Long[] ids) {

        for(Long id : ids) {
            this.removeById(id);
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorService.remove(lambdaQueryWrapper);
        }

    }



}
